<?php
use Overtrue\Pinyin\Pinyin;
require_once 'vendor/autoload.php';
$pinyin = new Pinyin();

$class = 7;
$out = 'C:\Program Files (x86)\Typeeasy\course\cn\syllable\\' . $class .'.xml';
$str = "";
for($i=1;$i<$argc;$i++){

    $str .= file_get_contents($argv[$i]);
}


$kong = array(" ","　","\t","\n","\r");
$str = str_replace($kong,'',$str);
//var_dump($str);
$str_arr = str_split_unicode($str);
$py_arr_unicode = $pinyin->convert($str,PINYIN_UNICODE);
$py_arr = $pinyin->convert($str);
$line = '';
for($i=0;$i<count($str_arr);$i++){
    @$line .= '<item display="'. $py_arr_unicode[$i] .'" display1="' . $str_arr[$i] . '" id="' . $i . '" comparision="' . $py_arr[$i] . '"/>'. PHP_EOL;
}

$out_content = <<<EOF
<!DOCTYPE x{$class}.xml>
<course>
    <courseInfo security="Q=" new="false" alias="汉语一级4" courseType="cn-syllable"/>
    <content>
$line    </content>
</course>
EOF;

$res = file_put_contents($out,$out_content);

if($res!==false)
{
    echo '本次导入 ' . count($str_arr) . ' 个字';
}else{
    die('error!');
}



function str_split_unicode($str, $length = 1) {
    $tmp = preg_split('~~u', $str, -1, PREG_SPLIT_NO_EMPTY);
    if ($length > 1) {
        $chunks = array_chunk($tmp, $length);
        foreach ($chunks as $i => $chunk) {
            $chunks[$i] = join('', (array) $chunk);
        }
        $tmp = $chunks;
    }
    return $tmp;
}